package com.lh.springcloud.alibaba.service.impl;


import com.lh.springcloud.alibaba.mapper.AccountMapper;
import com.lh.springcloud.alibaba.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

@Service
public class AccountServiceImpl implements AccountService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Autowired
    private AccountMapper accountMapper;

    //扣减账户余额
    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("开始扣减账户余额");

        //模拟线程异常，全局事务回滚
        //暂停秒钟线程
        try {
            TimeUnit.SECONDS.sleep(20);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        accountMapper.decrease(userId, money);
        LOGGER.info("结束扣减账户余额");
    }
}
